import 'package:get/get.dart';
import 'package:ride_rex/main/home/home_logic.dart';
import 'package:ride_rex/main/video/video_logic.dart';
import 'package:ride_rex/main/video/video_page.dart';
import 'package:ride_rex/sqlite/db_manager.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import '../generated/assets.dart';
import '../service/config_service.dart';
import '../ui/common_home_scaffold.dart';
import 'discover/discover_logic.dart';
import 'discover/discover_page.dart';
import 'home/home_page.dart';
import 'mine/mine_logic.dart';
import 'mine/mine_page.dart';

class MainLogic extends GetxController with LinkNavigator, refreshUnread {
  bool hasInitData = false;

  @override
  void onReady() {
    super.onReady();
    initData();
  }

  @override
  void onClose() {
    Get.delete<HomeLogic>();
    Get.delete<VideoLogic>();
    Get.delete<DiscoverLogic>();
    Get.delete<MineLogic>();
    DbManager.closeDb();
    super.onClose();
  }

  void initData() async {
    await DbManager.openDb();

    hasInitData = true;

    update();

    CommonConfigService.instance.init();
  }

  @override
  void refreshDataAnyway() {}
}

///刷新未读
mixin refreshUnread on LinkNavigator {
  @override
  String badgeOfHome() {
    return '';
  }

  @override
  String badgeOfVideo() {
    return '';
  }

  @override
  String badgeOfDiscover() {
    return '';
  }

  @override
  String badgeOfMine() {
    return '';
  }
}

///底部tab
mixin LinkNavigator implements CommonLife {
  final CommonHomeScaffoldController controller = CommonHomeScaffoldController(0);

  final _homePage = HomePage(Get.put(HomeLogic()));
  final _videoPage = VideoPage(Get.put(VideoLogic()));
  final _discoverPage = DiscoverPage(Get.put(DiscoverLogic()));
  final _minePage = MinePage(Get.put(MineLogic()));

  HomeLogic get homeLogic => _homePage.logic;

  VideoLogic get customLogic => _videoPage.logic;

  DiscoverLogic get dataLogic => _discoverPage.logic;

  MineLogic get myLogic => _minePage.logic;

  int _curPage = 0;

  int get curPage => _curPage;

  List<CommonHomeScaffoldItem> get pages => [
        CommonHomeScaffoldItem(
          _homePage,
          'Home',
          Assets.svgTabHomeNormal,
          Assets.svgTabHomeSelect,
          badgeOfHome,
          homeLogic,
        ),
        CommonHomeScaffoldItem(
          _videoPage,
          'Video',
          Assets.svgTabVideoNormal,
          Assets.svgTabVideoSelect,
          badgeOfVideo,
          customLogic,
        ),
        CommonHomeScaffoldItem(
          _discoverPage,
          'Discover',
          Assets.svgTabDiscoverNormal,
          Assets.svgTabDiscoverSelect,
          badgeOfDiscover,
          dataLogic,
        ),
        CommonHomeScaffoldItem(
          _minePage,
          'Mine',
          Assets.svgTabMineNormal,
          Assets.svgTabMineSelect,
          badgeOfMine,
          myLogic,
        ),
      ];

  void changePage(int page) {
    page = page >= pages.length ? pages.length - 1 : page;
    page = page < 0 ? 0 : page;
    pageLeave();
    _curPage = page;
    pageBack();
  }

  ///切换tab
  void changeTabIndex(index) {
    if (index < 0 || index >= pages.length) return;
    controller.changeIndex(index);
  }

  CommonLife get _curLogic => pages[_curPage].logic;

  ///页面离开
  @override
  void pageLeave() {
    _curLogic.pageLeave();
  }

  ///页面重显
  @override
  void pageBack() {
    _curLogic.pageBack();
    refreshDataAnyway();
  }

  ///页面关闭
  @override
  void pageClose() {
    _curLogic.pageClose();
  }

  void refreshDataAnyway();

  String badgeOfHome();

  String badgeOfVideo();

  String badgeOfDiscover();

  String badgeOfMine();
}
